package com.atguigu;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**********************
 *
 *@Author 闫星忠
 *@Time 2021/9/7  18:46
 *
 */
@Component
public class MyUserDetailService implements UserDetailsService {

    static Map<String ,User> map = new HashMap<>();
    static {
        User yxz = new User("yxz", "$2a$10$btzeNInhiP7X6mlwGsJqMeICIyTvN.q6A.QPm5it5w6z26U8gjNuu", true, 25);
        map.put(yxz.getUsername(),yxz);
    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = map.get(username);
        if (user == null) {
            return null;
        }


        Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ABC"));
        return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),authorities);
    }
}
